{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nnmf\n",
    "import urllib\n",
    "from numpy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from pandas_datareader import data \n",
    "#import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tickers=['YHOO','AVP','BIIB','BP','CL','CVX',\n",
    "#\t\t 'DNA','EXPE','GOOG','PG','XOM','AMGN']\n",
    "#tickers=['GOOG','AVP']\n",
    "tickers=['600848','600012','600688']\n",
    "shortest=300\n",
    "prices={}\n",
    "volumes={}\n",
    "dates={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "#start = datetime.datetime(2020, 9, 1)\n",
    "start ='2020-01-05'\n",
    "#end = datetime.date.today()\n",
    "end='2020-06-09'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    }
   ],
   "source": [
    "rows = ts.get_hist_data('600848', start, end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(102, 14)"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shape(rows.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.402000e+01,  2.425000e+01,  2.359000e+01,  2.353000e+01,\n",
       "        5.605206e+04, -5.400000e-01, -2.240000e+00,  2.421000e+01,\n",
       "        2.450800e+01,  2.444100e+01,  4.661265e+04,  5.848053e+04,\n",
       "        5.637507e+04,  6.200000e-01])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rows.values[88]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n",
      "error:  600848\n",
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n",
      "error:  600012\n",
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n",
      "error:  600688\n"
     ]
    }
   ],
   "source": [
    "for t in tickers:\n",
    "  # Open the URL\n",
    "\ttry:\n",
    "#\t\trows = data.DataReader(t, start, end)\n",
    "\t\trows = ts.get_hist_data(t, start, end)  \n",
    "\t\t#print(rows)\n",
    "\t\t# Extract the volume field from every line\n",
    "\t\tprices[t]=[float(r) for r in rows.close]\n",
    "\t\tvolumes[t]=[float(r) for r in rows.volume]\n",
    "\t\tdates[t]=[float(r) for r in rows.date]\n",
    "\texcept:\n",
    "\t\tprint('error: ',t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(dates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=len(volumes['600848'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1=[[prices[tickers[i]][j] \n",
    "\tfor i in range(len(tickers))] \n",
    "\tfor j in range(period)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[21.9, 5.23, 3.72],\n",
       " [21.85, 5.23, 3.75],\n",
       " [21.18, 5.26, 3.67],\n",
       " [20.84, 5.18, 3.61],\n",
       " [20.87, 5.21, 3.64],\n",
       " [21.16, 5.27, 3.66],\n",
       " [20.63, 5.23, 3.61],\n",
       " [20.28, 5.21, 3.56],\n",
       " [20.14, 5.15, 3.63],\n",
       " [20.57, 5.14, 3.66],\n",
       " [20.29, 5.15, 3.7],\n",
       " [20.21, 5.13, 3.69],\n",
       " [20.8, 5.13, 3.7],\n",
       " [21.19, 5.19, 3.84],\n",
       " [20.58, 5.19, 3.89],\n",
       " [20.58, 5.19, 3.96],\n",
       " [20.53, 5.16, 3.91],\n",
       " [20.61, 5.18, 3.87],\n",
       " [20.18, 5.21, 3.86],\n",
       " [20.29, 5.27, 3.93],\n",
       " [20.06, 5.24, 3.95],\n",
       " [20.3, 5.27, 3.96],\n",
       " [20.58, 5.33, 3.99],\n",
       " [20.53, 5.28, 3.98],\n",
       " [20.15, 5.31, 4.03],\n",
       " [19.7, 5.36, 4.02],\n",
       " [19.19, 5.39, 3.95],\n",
       " [19.21, 5.33, 4.07],\n",
       " [19.22, 5.21, 4.19],\n",
       " [19.23, 5.13, 4.3],\n",
       " [19.56, 5.15, 4.52],\n",
       " [19.52, 5.16, 4.22],\n",
       " [19.68, 5.14, 4.25],\n",
       " [19.65, 5.21, 4.16],\n",
       " [19.52, 5.16, 4.16],\n",
       " [19.44, 5.16, 4.15],\n",
       " [19.51, 5.17, 4.22],\n",
       " [19.35, 5.2, 4.21],\n",
       " [19.11, 5.15, 4.18],\n",
       " [19.51, 5.13, 4.11],\n",
       " [19.57, 5.16, 4.23],\n",
       " [19.55, 5.16, 4.21],\n",
       " [19.76, 5.18, 4.28],\n",
       " [19.55, 5.1, 4.12],\n",
       " [19.6, 5.12, 4.15],\n",
       " [19.49, 5.09, 3.77],\n",
       " [20.1, 5.11, 3.89],\n",
       " [19.6, 5.16, 3.9],\n",
       " [18.85, 5.26, 3.99],\n",
       " [18.91, 5.22, 4.01],\n",
       " [19.23, 5.31, 4.07],\n",
       " [18.9, 5.23, 3.94],\n",
       " [18.65, 5.13, 3.9],\n",
       " [19.24, 5.29, 4.19],\n",
       " [19.2, 5.2, 4.16],\n",
       " [19.2, 5.22, 4.27],\n",
       " [19.4, 5.27, 4.23],\n",
       " [19.8, 5.35, 4.48],\n",
       " [20.18, 5.42, 4.15],\n",
       " [20.66, 5.56, 4.41],\n",
       " [21.03, 5.91, 4.9],\n",
       " [20.89, 6.18, 5.24],\n",
       " [20.81, 5.77, 4.95],\n",
       " [21.46, 5.8, 4.5],\n",
       " [21.64, 5.64, 4.09],\n",
       " [21.47, 5.54, 4.12],\n",
       " [21.3, 5.34, 4.18],\n",
       " [21.08, 5.3, 4.16],\n",
       " [20.41, 5.2, 4.0],\n",
       " [21.39, 5.35, 3.8],\n",
       " [21.55, 5.38, 3.76],\n",
       " [21.77, 5.37, 3.67],\n",
       " [22.43, 5.42, 3.81],\n",
       " [22.62, 5.5, 3.69],\n",
       " [21.83, 5.5, 3.69],\n",
       " [21.6, 5.39, 3.64],\n",
       " [21.76, 5.43, 3.65],\n",
       " [21.68, 5.5, 3.63],\n",
       " [21.13, 5.5, 3.56],\n",
       " [21.05, 5.48, 3.57],\n",
       " [21.21, 5.43, 3.6],\n",
       " [21.11, 5.36, 3.57],\n",
       " [21.27, 5.33, 3.57],\n",
       " [20.88, 5.28, 3.55],\n",
       " [20.81, 5.31, 3.51],\n",
       " [20.8, 5.22, 3.49],\n",
       " [19.97, 5.13, 3.41],\n",
       " [21.23, 5.04, 3.49],\n",
       " [23.59, 5.6, 3.88],\n",
       " [24.13, 5.75, 3.93],\n",
       " [23.99, 5.82, 3.97],\n",
       " [24.67, 5.89, 4.0],\n",
       " [24.67, 5.84, 4.0],\n",
       " [24.87, 5.82, 3.98],\n",
       " [24.78, 5.9, 4.02],\n",
       " [25.14, 5.94, 4.07],\n",
       " [24.58, 5.95, 4.07],\n",
       " [24.66, 5.92, 4.06],\n",
       " [24.48, 5.95, 4.09],\n",
       " [24.27, 5.9, 4.23],\n",
       " [24.78, 5.95, 4.12],\n",
       " [24.62, 5.9, 4.27]]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "43329.273020962944\n",
      "24.40607599750159\n",
      "4.971094623949186\n",
      "3.3232621636769184\n",
      "2.983028945139608\n",
      "[[ 5.50927019  1.93983132  1.58672685]\n",
      " [ 1.27543607  1.61571932  1.5670842 ]\n",
      " [ 0.61361899  1.68533745  1.87104902]\n",
      " [12.82613514  1.77994873  0.75115837]\n",
      " [17.12158931  2.16817525  0.91065553]]\n",
      "[[5.47936183e-01 8.40251719e-02 9.75442222e-01 2.60694458e-02\n",
      "  1.04238014e+00]\n",
      " [1.12555894e+00 6.65263374e-01 2.30840824e-02 7.04433067e-01\n",
      "  3.35644867e-01]\n",
      " [1.00078589e+00 1.12195411e-01 5.88495610e-01 9.07898473e-01\n",
      "  2.05955396e-01]\n",
      " [3.99633478e-01 5.99238285e-01 5.69736146e-01 1.29994465e+00\n",
      "  4.99663382e-02]\n",
      " [9.01253448e-01 7.34054505e-01 1.26137433e-01 9.10732513e-01\n",
      "  1.87665444e-01]\n",
      " [9.35874389e-01 6.45697488e-02 7.19550477e-01 2.65006126e-01\n",
      "  7.06317355e-01]\n",
      " [3.66698293e-01 7.83824339e-01 5.09062198e-01 5.53286857e-01\n",
      "  5.96319951e-01]\n",
      " [6.58362944e-01 2.15616716e-01 7.60717139e-01 7.85066057e-01\n",
      "  3.41998413e-01]\n",
      " [6.25763405e-01 6.45160206e-01 4.52437061e-01 4.51439190e-01\n",
      "  5.72864341e-01]\n",
      " [3.33075980e-01 4.85275860e-01 7.72457318e-01 6.98794167e-01\n",
      "  5.07201048e-01]\n",
      " [9.52303848e-01 6.16983500e-01 2.35353510e-01 6.58972095e-01\n",
      "  3.30711541e-01]\n",
      " [5.62667059e-01 2.86502397e-01 7.76145888e-01 1.03845711e+00\n",
      "  1.72463075e-01]\n",
      " [4.46010456e-01 7.65156316e-01 4.59424240e-01 5.24701114e-01\n",
      "  6.04809510e-01]\n",
      " [7.48445175e-01 4.82750420e-01 5.10338438e-01 6.15600647e-01\n",
      "  4.81210237e-01]\n",
      " [5.97361177e-01 1.48103657e-01 9.60052258e-01 5.34156127e-01\n",
      "  5.64174067e-01]\n",
      " [6.98793807e-01 5.99157264e-01 5.04429690e-01 7.90900511e-01\n",
      "  3.21517309e-01]\n",
      " [4.98442111e-02 1.13936695e+00 5.19789768e-01 4.72491946e-01\n",
      "  7.25209761e-01]\n",
      " [2.96594076e-01 5.18608985e-01 8.77373913e-01 1.74907650e-01\n",
      "  9.07209249e-01]\n",
      " [9.60118911e-01 7.11644326e-01 2.41207375e-01 3.19784049e-01\n",
      "  5.68451208e-01]\n",
      " [6.67847097e-01 1.08838634e-01 1.01386373e+00 1.65146846e-01\n",
      "  8.02253964e-01]\n",
      " [3.07623912e-01 6.21515250e-01 7.81425660e-01 1.24548190e+00\n",
      "  6.51158577e-02]\n",
      " [2.49405798e-01 1.00342306e+00 5.71852035e-01 7.92579521e-02\n",
      "  9.50711630e-01]\n",
      " [5.29143575e-01 3.84331701e-01 8.60602294e-01 7.91623242e-01\n",
      "  3.79222636e-01]\n",
      " [1.20361818e+00 5.31823451e-01 2.57676251e-01 2.66541093e-01\n",
      "  5.63061890e-01]\n",
      " [6.27523134e-01 3.79502980e-01 8.45874979e-01 4.02802048e-01\n",
      "  6.14628014e-01]\n",
      " [6.36993090e-01 2.01558311e-01 1.01734907e+00 5.07062637e-01\n",
      "  5.14611775e-01]\n",
      " [1.24434052e+00 2.81682781e-01 4.91759556e-01 8.50692824e-01\n",
      "  4.50265649e-02]\n",
      " [5.00692390e-01 5.70893663e-02 1.27097101e+00 5.35443706e-01\n",
      "  5.10254328e-01]\n",
      " [1.66371511e-01 7.22204789e-01 9.44297611e-01 6.61603323e-01\n",
      "  4.85240352e-01]\n",
      " [1.15863391e+00 7.23844979e-01 2.06147150e-01 7.97419226e-01\n",
      "  9.07458064e-02]\n",
      " [9.73946968e-01 2.76788863e-01 8.00413649e-01 6.31963481e-01\n",
      "  3.04733606e-01]\n",
      " [3.19018286e-01 5.50131166e-01 9.61206617e-01 6.37059078e-01\n",
      "  4.84003637e-01]\n",
      " [3.14723738e-01 6.45302347e-02 1.39191903e+00 3.80698562e-01\n",
      "  7.07610758e-01]\n",
      " [5.51693964e-01 1.35239937e+00 1.22076314e-01 1.36481400e-01\n",
      "  7.62138315e-01]\n",
      " [3.23355727e-01 8.87278820e-01 6.68006403e-01 3.69766568e-01\n",
      "  6.68329748e-01]\n",
      " [6.15697522e-01 8.05020408e-01 5.14793631e-01 5.11214758e-01\n",
      "  4.75224535e-01]\n",
      " [7.54596715e-01 1.24930661e+00 7.05258573e-02 1.91408489e-01\n",
      "  6.56861185e-01]\n",
      " [4.40177468e-01 6.27171398e-01 8.27080914e-01 5.65645863e-01\n",
      "  4.87784877e-01]\n",
      " [3.97858593e-01 8.58065607e-01 6.36689984e-01 7.69130816e-01\n",
      "  3.24437000e-01]\n",
      " [9.46866784e-01 4.85666046e-01 5.03012951e-01 7.76254783e-01\n",
      "  1.98399218e-01]\n",
      " [3.12748272e-01 5.01344565e-01 1.00926239e+00 6.19237392e-01\n",
      "  5.04194004e-01]\n",
      " [8.94740759e-01 4.32296714e-01 6.13303750e-01 9.47750321e-01\n",
      "  8.88542875e-02]\n",
      " [1.11294243e+00 1.20325583e-01 7.37931260e-01 9.18446737e-01\n",
      "  7.15445865e-02]\n",
      " [8.52206638e-01 2.44477258e-01 7.74762555e-01 7.45164772e-01\n",
      "  2.62689148e-01]\n",
      " [4.49541271e-01 6.44795297e-01 7.65012569e-01 3.65394158e-01\n",
      "  6.50218150e-01]\n",
      " [1.13833831e-01 1.96515093e-01 1.27220493e+00 4.20367558e-01\n",
      "  7.26925695e-01]\n",
      " [1.53970747e-01 9.18087390e-01 6.61838350e-01 4.80001479e-02\n",
      "  9.96141106e-01]\n",
      " [2.24229440e-01 1.24027933e+00 3.44171336e-01 5.54387146e-01\n",
      "  5.52330122e-01]\n",
      " [3.57070665e-01 6.48703739e-01 8.73599179e-01 1.09758178e-01\n",
      "  8.24463477e-01]\n",
      " [3.61708821e-01 4.85087834e-01 9.69169213e-01 6.22119077e-01\n",
      "  4.51208814e-01]\n",
      " [4.24661529e-01 7.08182550e-01 7.55259325e-01 6.81314801e-01\n",
      "  3.96253637e-01]\n",
      " [1.57228539e+00 4.43188992e-01 9.78512287e-02 5.70890247e-01\n",
      "  1.33849512e-01]\n",
      " [6.74810987e-01 3.78686726e-01 8.04437047e-01 3.17602129e-01\n",
      "  5.77334676e-01]\n",
      " [1.28563706e-01 1.07486885e+00 7.08735051e-01 3.88892703e-01\n",
      "  6.85194117e-01]\n",
      " [5.00188890e-01 7.97341788e-01 6.44656624e-01 3.30029593e-01\n",
      "  6.30217790e-01]\n",
      " [4.99152309e-01 3.49521628e-01 1.05640979e+00 5.08833964e-01\n",
      "  5.15116766e-01]\n",
      " [1.81393139e-01 2.71316067e-01 1.29651777e+00 1.31812311e+00\n",
      "  2.00921550e-02]\n",
      " [4.27358281e-01 1.98247862e-01 1.30325932e+00 5.85107431e-01\n",
      "  5.18320343e-01]\n",
      " [2.61654355e-01 1.04853398e+00 6.16149339e-01 2.81672565e-01\n",
      "  7.83102210e-01]\n",
      " [1.08887615e-01 6.12152137e-01 1.20081815e+00 1.82761120e-01\n",
      "  9.45846369e-01]\n",
      " [4.88421808e-01 8.71787774e-01 9.22399017e-01 2.39865247e-01\n",
      "  7.92699214e-01]\n",
      " [1.14679051e+00 1.33461723e+00 1.76289385e-01 9.41441441e-01\n",
      "  3.90197172e-02]\n",
      " [8.18903323e-01 4.48619556e-01 1.03009371e+00 2.35717454e-01\n",
      "  7.03909799e-01]\n",
      " [5.44472102e-01 7.77521077e-01 7.63639242e-01 6.19804000e-01\n",
      "  5.28340073e-01]\n",
      " [4.76515567e-01 8.87045312e-01 5.48655171e-01 5.26714117e-01\n",
      "  6.30472947e-01]\n",
      " [3.37590098e-01 7.36895850e-01 7.68236167e-01 5.41610702e-01\n",
      "  6.57201494e-01]\n",
      " [7.93895608e-01 4.60189117e-01 6.60497772e-01 2.39071114e-01\n",
      "  7.51007717e-01]\n",
      " [5.48188612e-01 7.37600104e-01 6.01315437e-01 1.88900027e-01\n",
      "  8.36215307e-01]\n",
      " [2.39250610e-01 9.84390115e-01 5.64000905e-01 3.68998295e-01\n",
      "  7.44709170e-01]\n",
      " [8.61167586e-01 7.04857056e-01 2.77924307e-01 3.54020952e-01\n",
      "  6.44706274e-01]\n",
      " [1.79437692e+00 1.10306900e-01 8.84369085e-02 8.85686652e-01\n",
      "  5.15959080e-03]\n",
      " [3.72608271e-01 1.09213944e+00 2.51588539e-01 3.88882887e-01\n",
      "  7.70287086e-01]\n",
      " [8.14946952e-01 7.52372876e-01 2.11336288e-01 7.85542051e-01\n",
      "  3.95801737e-01]\n",
      " [7.18677060e-01 6.49414490e-01 3.67804541e-01 4.75327133e-01\n",
      "  6.72838163e-01]\n",
      " [2.06588619e-01 8.47875798e-01 6.32113473e-01 2.02654883e-01\n",
      "  9.71748802e-01]\n",
      " [3.20827936e-01 1.81134653e-01 1.04960393e+00 7.07019455e-01\n",
      "  5.78492594e-01]\n",
      " [7.57664026e-01 5.98542052e-01 3.95779353e-01 4.60497867e-01\n",
      "  6.24070346e-01]\n",
      " [5.30171442e-01 9.37231732e-01 2.94228439e-01 4.22333003e-01\n",
      "  6.99752860e-01]\n",
      " [5.42253269e-01 1.06944218e+00 1.45625292e-01 9.68613616e-01\n",
      "  2.50064080e-01]\n",
      " [8.93490514e-01 3.95613280e-01 4.72834970e-01 7.62789261e-01\n",
      "  3.25363535e-01]\n",
      " [4.81279721e-01 6.31329357e-01 5.83277607e-01 4.60696165e-01\n",
      "  6.71864471e-01]\n",
      " [9.14409998e-01 5.30923899e-01 3.01907466e-01 8.51943539e-01\n",
      "  2.50920231e-01]\n",
      " [9.21936040e-01 3.39527599e-01 4.86539176e-01 1.80200672e-01\n",
      "  7.68780730e-01]\n",
      " [7.63725292e-01 7.42659179e-01 2.19120781e-01 8.03879665e-01\n",
      "  3.08997702e-01]\n",
      " [2.28101963e-01 3.55440124e-01 9.35364578e-01 8.99340790e-01\n",
      "  4.09404584e-01]\n",
      " [2.99667169e-01 7.12469392e-01 5.40310863e-01 1.03499688e+00\n",
      "  2.71325390e-01]\n",
      " [7.76262916e-01 8.70292963e-01 1.05730460e-01 1.83774831e-01\n",
      "  7.11007429e-01]\n",
      " [6.79130684e-01 8.05460165e-01 1.29333451e-01 7.21347359e-01\n",
      "  4.16515447e-01]\n",
      " [3.58788721e-01 9.85297771e-01 3.99807802e-01 1.87472719e-01\n",
      "  1.03439819e+00]\n",
      " [1.46894796e-01 1.05228857e+00 4.37575644e-01 1.57750949e+00\n",
      "  8.63656100e-02]\n",
      " [1.58339776e-01 4.96867737e-01 9.46205724e-01 1.50092448e+00\n",
      "  1.55410838e-01]\n",
      " [1.04338195e+00 6.35662497e-01 1.92919021e-01 1.18142692e+00\n",
      "  1.66052586e-01]\n",
      " [7.82196365e-01 8.34429916e-01 2.27457325e-01 7.30738940e-01\n",
      "  5.71734859e-01]\n",
      " [7.08814773e-01 6.90160517e-01 3.94138457e-01 5.83566019e-01\n",
      "  7.22041132e-01]\n",
      " [8.94696672e-01 2.19363250e-01 7.21400080e-01 2.07765921e-01\n",
      "  9.62218670e-01]\n",
      " [4.15013800e-01 4.71816960e-01 8.25076573e-01 8.62038473e-01\n",
      "  6.24709060e-01]\n",
      " [1.06216837e+00 5.14026681e-01 3.72215405e-01 4.39764253e-01\n",
      "  7.13232261e-01]\n",
      " [5.87497440e-01 7.25707243e-02 1.03698686e+00 1.13223728e+00\n",
      "  3.61063836e-01]\n",
      " [1.21691379e+00 5.09945688e-01 2.27618031e-01 1.16081631e+00\n",
      "  1.22676554e-01]\n",
      " [1.24453257e+00 8.78349310e-01 1.78955517e-03 3.59928439e-02\n",
      "  9.24586573e-01]\n",
      " [7.08296738e-01 5.49331111e-01 5.38371287e-01 1.49651736e+00\n",
      "  3.82730882e-02]\n",
      " [1.25416851e+00 5.41661161e-01 2.74521225e-01 1.05589144e-01\n",
      "  9.04955964e-01]]\n"
     ]
    }
   ],
   "source": [
    "w,h=nnmf.factorize(matrix(l1),pc=5)\n",
    "\n",
    "print (h)\n",
    "print (w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3)"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shape(h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(102, 5)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shape(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Feature 0\n",
      "(5.509270185736772, '600848')\n",
      "(1.9398313166520826, '600012')\n",
      "(1.5867268464435365, '600688')\n",
      "\n",
      "stock:  600848 date: 70 special movement\n",
      "Feature 1\n",
      "(1.6157193175134321, '600012')\n",
      "(1.567084199949925, '600688')\n",
      "(1.2754360667615536, '600848')\n",
      "\n",
      "stock:  600012 date: 33 special movement\n",
      "Feature 2\n",
      "(1.8710490169559053, '600688')\n",
      "(1.6853374505331606, '600012')\n",
      "(0.6136189922075239, '600848')\n",
      "\n",
      "stock:  600688 date: 32 special movement\n",
      "Feature 3\n",
      "(12.826135140296522, '600848')\n",
      "(1.7799487323517755, '600012')\n",
      "(0.7511583695159464, '600688')\n",
      "\n",
      "stock:  600848 date: 89 special movement\n",
      "Feature 4\n",
      "(17.121589311189556, '600848')\n",
      "(2.168175245180197, '600012')\n",
      "(0.9106555310651251, '600688')\n",
      "\n",
      "stock:  600848 date: 0 special movement\n"
     ]
    }
   ],
   "source": [
    "# Loop over all the features\n",
    "for i in range(shape(h)[0]):\n",
    "    print (\"Feature %d\" %i)\n",
    "\n",
    "    # Get the top stocks for this feature\n",
    "    ol=[(h[i,j],tickers[j]) for j in range(shape(h)[1])]\n",
    "    ol.sort()\n",
    "    ol.reverse()\n",
    "    \n",
    "    for j in range(len(tickers)):\n",
    "        print(ol[j])\n",
    "    print()\n",
    "\n",
    "    # Show the top dates for this feature\n",
    "    # i idicate feature\n",
    "    porder=[(w[d,i],d) for d in range(period)]\n",
    "    porder.sort()\n",
    "    porder.reverse()\n",
    "    #p[0] top dates weight, p[1] date\n",
    "    #print ((p,p[1]) for p in porder[0:3]])\n",
    "    #print top 3\n",
    "    #print('porder:',porder[0:3])\n",
    "    print('stock: ',ol[0][1],'date:',porder[0][1], 'special movement')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21.55"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices['600848'][70]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 5.50927019,  1.93983132,  1.58672685],\n",
       "        [ 1.27543607,  1.61571932,  1.5670842 ],\n",
       "        [ 0.61361899,  1.68533745,  1.87104902],\n",
       "        [12.82613514,  1.77994873,  0.75115837],\n",
       "        [17.12158931,  2.16817525,  0.91065553]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
